package org.oneblog.data.util;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Smile.Wu
 * @version 2015-9-10
 */
public class SolrQueryUtil {

    /**
     * 
     * @param list
     * @param field
     * @param operator
     * @return ${field}:(${list.get(0)} ${operator} ${list.get(1)})
     */
    public static <T> String buildQuery(List<T> list, String field, String operator) {
        StringBuilder query = new StringBuilder("");
        String op = " " + operator + " ";
        if (list != null && list.size() > 0) {
            query.append("(");
            boolean isFirst = true;
            for (T t : list) {
                if (isFirst) {
                    isFirst = false;
                } else {
                    query.append(op);
                }
                query.append(field).append(":").append(String.valueOf(t));
            }
            query.append(")");
        }
        return query.toString();
    }

    public static <T> String buildQuery(List<T> list, String field, String operator, int weight) {
        return buildQuery(list, field, operator, weight, 0);
    }

    public static <T> String buildQuery(List<T> list, String field, String operator, int weight, int delt) {
        StringBuilder query = new StringBuilder("");
        String op = " " + operator + " ";
        if (list != null && list.size() > 0) {
            query.append("(");
            boolean isFirst = true;
            for (T t : list) {
                if (isFirst) {
                    isFirst = false;
                } else {
                    query.append(op);
                }
                query.append(field).append(":").append(String.valueOf(t)).append("^").append(weight);

                weight += delt;
                if (weight < 0) {
                    weight = 1;
                }
            }
            query.append(")");
        }
        return query.toString();
    }

    public static <T> String buildQueryNumber(List<? extends Number> list, String field, String operator, int weight, int delt) {
        StringBuilder query = new StringBuilder("");
        String op = " " + operator + " ";
        if (list != null && list.size() > 0) {
            query.append("(");
            boolean isFirst = true;
            for (Number t : list) {
                if (isFirst) {
                    isFirst = false;
                } else {
                    query.append(op);
                }
                double a = StringTools.getDbl((Object) t, -1D);
                if (a > 0) {
                    query.append(field).append(":").append((long) a).append("^").append(weight);
                    weight += delt;
                    if (weight < 0) {
                        weight = 1;
                    }
                }
            }
            query.append(")");
        }
        return query.toString();
    }

    public static String buildQuery(String longValueString, String field, String operator) {
        if (StringTools.isBlank(longValueString)) {
            return "";
        }
        List<Long> ids = new ArrayList<>(5);
        String[] values = longValueString.split(",");
        for (String v : values) {
            long id = StringFormat.getLong(v, -1L);
            if (id > 0) {
                ids.add(id);
            }
        }

        return buildQuery(ids, field, operator);
    }

    // map(min(product(sub(maxField,min),sub(max,minField)),1),0,2,1,0)
    public static String buildAnnualSalaryQuery(String minField, String maxField, double min, double max, int weight) {
        if (min < 1 || max < 1) {
            return "";
        }
        return "map(min(product(sub(" + maxField + "," + min + "),sub(" + max + "," + minField + ")),1),0,2,1,0)^" + weight;
    }
}
